#!/usr/bin/env bash

help(){
    cat<<__EOF__
Usage: m group [OPTIONS] [ARGUMENTS]

Description: Manage groups on macOS.

Options:
  --help                     Show this help message
  --list                     List all Groups
  --info GROUP               Show information about a specific groups
  --adduser USER GROUP       Add an user to a specific groups
  --removeuser USER GROUP    Remove an user from a specific Groups
  --ismember USER GROUP      Check if an user is a member of a specific group
__EOF__
}

group_ls(){
    dscl . -list /Groups gid
}

group_info(){
    [ -z "$1" ] && help && exit 1
    dscl . -read "/Groups/$1"
}

group_add_user(){
    [ -z "$1" -o -z "$2" ] && help && exit 1
    sudo dseditgroup -o edit -a ${1} -t user ${2}
}

group_remove_user(){
    [ -z "$1" -o -z "$2" ] && help && exit 1
    sudo dseditgroup -o edit -d ${1} -t user ${2}
}

group_is_a_member(){
    [ -z "$1" -o -z "$2" ] && help && exit 1
    dseditgroup -o checkmember -m ${1} ${2}
}


# Main script execution
option="${1}"
shift

case "${option}" in
    --help)
        help
        ;;
    --list)
        group_ls
        ;;
    --info)
        group_info $@
        ;;
    --adduser)
        group_add_user $@
        ;;
    --removeuser)
        group_remove_user $@
        ;;
    --ismember)
        group_is_a_member $@
        ;;
    *)
        help && exit 1
        ;;
esac
